package cn.tedu.fruitshop.service.impl;

import cn.tedu.fruitshop.ex.ServiceException;
import cn.tedu.fruitshop.mapper.InformMapper;
import cn.tedu.fruitshop.pojo.dto.CarUpdateInfoDTO;
import cn.tedu.fruitshop.pojo.dto.InformAddNewDTO;
import cn.tedu.fruitshop.pojo.dto.InformUpdateInfoDTO;
import cn.tedu.fruitshop.pojo.entity.Inform;
import cn.tedu.fruitshop.pojo.vo.InformListItemVO;
import cn.tedu.fruitshop.pojo.vo.InformStandardVO;
import cn.tedu.fruitshop.service.InformService;
import cn.tedu.fruitshop.web.JsonResult;
import cn.tedu.fruitshop.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Slf4j
@Service
public class InformServiceImpl implements InformService {

    @Autowired
    InformMapper informMapper;

    @Override
    public void addNew(InformAddNewDTO informAddNewDTO) {
        log.debug("开始处理【添加公告】的业务，参数：{}", informAddNewDTO);
        Inform inform=new Inform();
        BeanUtils.copyProperties(informAddNewDTO,inform);
        log.debug("即将插入公告数据：{}", inform);
        int rows = informMapper.insert(inform);
        if(rows!=1){
            String message = "服务器炸了,添加数据失败";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void delete(Integer id) {
        log.debug("开始处理【删除公告】的业务，参数：{}", id);
        InformStandardVO queryResult=informMapper.getStandardById(id);
        if(queryResult==null){
            String message = "删除公告失败,尝试访问的数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = informMapper.deleteById(id);
        if(rows!=1){
            String message="服务器炸了,删除数据失败";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public List<InformListItemVO> list() {
        return informMapper.list();
    }

    @Override
    public void updateInfoById(Integer id, InformUpdateInfoDTO informUpdateInfoDTO) {
        log.debug("开始处理【修改公告详情】的业务，参数ID：{}, 新数据：{}", id, informUpdateInfoDTO);
        InformStandardVO queryResult = informMapper.getStandardById(id);
        if(queryResult==null){
            String message = "修改公告详情失败！尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        Inform inform=new Inform();
        BeanUtils.copyProperties(informUpdateInfoDTO,inform);
        log.debug("即将修改数据：{}", inform);
        int rows = informMapper.update(inform);
        if (rows != 1) {
            String message = "修改公告详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }


}
